package de.blanky.madMonkeys.map;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.jws.Oneway;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

public class Map_Builder_MainFrame extends JFrame implements ActionListener {

	
	private JPanel contentPane;
	private ViewFrame view;
	private JMenuItem mntmNewmap;
	private JScrollPane scroller;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Map_Builder_MainFrame frame = new Map_Builder_MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Map_Builder_MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 929, 599);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(20, 0));
		
		view = new ViewFrame("default",0,0);
		
		JPanel chooser = new JPanel();
		contentPane.add(chooser, BorderLayout.WEST);
		chooser.setLayout(new GridLayout(3, 1, 0, 0));
		
		JPanel grass_panel = new JPanel();
		grass_panel.setBackground(Color.WHITE);
		chooser.add(grass_panel);
		
		ButtonGroup group = new ButtonGroup();
		
		JRadioButton grass_button = new JRadioButton();
		grass_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				view.setCurrentSelectd(ViewFrame.GRASS);
			}
		});
		grass_button.setBackground(Color.WHITE);
		
		JRadioButton stone_button = new JRadioButton();
		stone_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				view.setCurrentSelectd(ViewFrame.STONE);
			}
		});
		
		JRadioButton bush_button = new JRadioButton();
		bush_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				view.setCurrentSelectd(ViewFrame.BUSH);
			}
		});
		bush_button.setBackground(Color.WHITE);
		
		group.add(grass_button);
		group.add(stone_button);
		group.add(bush_button);
		grass_panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		
		grass_panel.add(grass_button);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBackground(Color.WHITE);
		lblNewLabel.setIcon(new ImageIcon(Map_Builder_MainFrame.class.getResource("/de/blanky/madMonkeys/map/res/jungle_1.png")));
		grass_panel.add(lblNewLabel);
		
		JPanel stone_panel = new JPanel();
		stone_panel.setBackground(Color.WHITE);
		chooser.add(stone_panel);
		stone_panel.setLayout(new GridLayout(0, 2, 0, 0));
		

		stone_button.setBackground(Color.WHITE);
		stone_panel.add(stone_button);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Map_Builder_MainFrame.class.getResource("/de/blanky/madMonkeys/map/res/stone_1.png")));
		label.setBackground(Color.RED);
		stone_panel.add(label);
		
		JPanel bush_panel = new JPanel();
		bush_panel.setBackground(Color.WHITE);
		chooser.add(bush_panel);
		bush_panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		
		bush_panel.add(bush_button);
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(Map_Builder_MainFrame.class.getResource("/de/blanky/madMonkeys/map/res/jungle_2.png")));
		label_1.setBackground(Color.WHITE);
		bush_panel.add(label_1);
		

		
		scroller = new JScrollPane(view);
		setTitle(view.getName());
		
		contentPane.add(scroller,BorderLayout.CENTER);
		
		view.setLayout(new GridLayout(1, 0, 0, 0));
		
		JMenuBar menuBar = new JMenuBar();
		contentPane.add(menuBar, BorderLayout.NORTH);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		mntmNewmap = new JMenuItem("new_Map");
		mntmNewmap.addActionListener(this);
		mnFile.add(mntmNewmap);
		
		
		
	}
	
	public void setView(ViewFrame f){
		contentPane.remove(scroller);
		scroller = new JScrollPane(f);
		scroller.getVerticalScrollBar().setUnitIncrement(12);
		scroller.getHorizontalScrollBar().setUnitIncrement(12);
		contentPane.add(scroller, BorderLayout.CENTER);
		
		view = f;
		scroller.validate();
		contentPane.validate();
		
		setTitle(view.getName());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() ==  mntmNewmap){
			Dialog_new_map newMap = new Dialog_new_map(this, view); 
		}
		
	}

}
